package pyrachnid.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import pyrachnid.ctrl.Simulator;
import pyrachnid.view.component.RobotTypeListModel;
import pyrachnid.view.component.RobotTypeTableModel;

public class TypeRobotWindow extends JDialog {

	private static final long serialVersionUID = -4649325956904795985L;
	private final JPanel contentPanel = new JPanel();
	private Simulator _simulator;
	private JTable _table_typeRobotParams;
	private JList _list_typeRobot;

	/**
	 * Create the dialog.
	 */
	public TypeRobotWindow(Simulator p_sim) {
		setResizable(false);

		_simulator = p_sim;

		setBounds(100, 100, 515, 506);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
		{
			JPanel panel_manage = new JPanel();
			contentPanel.add(panel_manage);
			panel_manage.setLayout(new GridLayout(0, 2, 0, 0));
			{
				JPanel panel_typeRobotList = new JPanel();
				panel_manage.add(panel_typeRobotList);
				panel_typeRobotList.setLayout(new BoxLayout(
						panel_typeRobotList, BoxLayout.Y_AXIS));
				{
					JPanel panel_List = new JPanel();
					panel_typeRobotList.add(panel_List);
					{
						_list_typeRobot = new JList();
						_list_typeRobot
								.setPreferredSize(new Dimension(250, 380));
						panel_List.add(_list_typeRobot);
					}
				}
				{
					JPanel panelRobotTypeControls = new JPanel();
					panel_typeRobotList.add(panelRobotTypeControls);
					panelRobotTypeControls
							.setLayout(new GridLayout(0, 2, 0, 0));
					{
						JButton btnAddNewType = new JButton("Nouveau");
						btnAddNewType.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
								_simulator.addRobotType();
								reloadList();
								refresh(true);
							}
						});
						btnAddNewType.setPreferredSize(new Dimension(75, 30));
						panelRobotTypeControls.add(btnAddNewType);
					}
					{
						JButton btnSupprimer = new JButton("Supprimer");
						btnSupprimer.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
								if (_simulator.removeRobotType(getSelectedRobotType())) {
									refresh();
								}
							}
						});
						btnSupprimer.setPreferredSize(new Dimension(81, 30));
						panelRobotTypeControls.add(btnSupprimer);
					}
				}
			}
			{
				JPanel panel_typeRobotParams = new JPanel();
				panel_manage.add(panel_typeRobotParams);
				panel_typeRobotParams.setLayout(new GridLayout(0, 1, 0, 0));
				{
					JScrollPane jscroll = new JScrollPane();
					panel_typeRobotParams.add(jscroll);
					jscroll.setLayout(new ScrollPaneLayout());
					{
						_table_typeRobotParams = new JTable();
						_table_typeRobotParams.setRowSelectionAllowed(false);
						_table_typeRobotParams.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
						_table_typeRobotParams.getTableHeader().setReorderingAllowed(false);
						_table_typeRobotParams.getModel().addTableModelListener(new TableModelListener() {
							@Override
							public void tableChanged(TableModelEvent e) {
								refreshList();
							}
						});
						jscroll.setViewportView(_table_typeRobotParams);
					}
				}
			}
		}
		{
			JPanel panelWinControls = new JPanel();
			contentPanel.add(panelWinControls);
			panelWinControls.setLayout(new GridLayout(0, 1, 0, 0));
			{
				JButton btnClose = new JButton("Fermer");
				btnClose.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				btnClose.setPreferredSize(new Dimension(200, 40));
				btnClose.setAlignmentX(Component.RIGHT_ALIGNMENT);
				btnClose.setActionCommand("OK");
				panelWinControls.add(btnClose);
				getRootPane().setDefaultButton(btnClose);
			}
		}

		this.initialize();
	}

	/**
	 * Initialize window elements
	 */
	private void initialize() {
		_list_typeRobot.setModel(new RobotTypeListModel(_simulator.getSenario()
				.getRobotTypeList()));
		_list_typeRobot.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent evt) {
				refreshTable();
			}
		});
		
		addWindowListener(new WindowAdapter() {
			public void windowClosed(WindowEvent e) {
				_simulator.refreshList();
			}
		});
	}

	/**
	 * Get selected robot type in window list
	 * 
	 * @return Index of selected robot type
	 */
	public int getSelectedRobotType() {
		return this._list_typeRobot.getSelectedIndex();
	}
	
	/**
	 * Refresh window items
	 */
	private void refresh() {
		/** List **/
		this.refreshList(false);

		/** Parameters table **/
		this.refreshTable();
	}

	/**
	 * Refresh window items
	 * @param p_newValue Select the last inserted value in list
	 */
	private void refresh(boolean p_newValue) {
		/** List **/
		this.refreshList(p_newValue);

		/** Parameters table **/
		this.refreshTable();
	}

	/**
	 * Refresh parameters table
	 */
	private void refreshTable() {
		if (this.getSelectedRobotType() >= 0) {
			_table_typeRobotParams.setModel(new RobotTypeTableModel(_simulator
					.getSenario().getRobotTypeList()
					.get(_list_typeRobot.getSelectedIndex())));
			_table_typeRobotParams.getModel().addTableModelListener(new TableModelListener() {
				@Override
				public void tableChanged(TableModelEvent e) {
					refreshList();
				}
			});
		} else {
			_table_typeRobotParams.setModel(new DefaultTableModel());
		}
	}
	
	/**
	 * Refresh robot type list
	 */
	private void refreshList() {
		this.refreshList(false);
	}

	/**
	 * Refresh robot type list
	 * @param p_newValue Select the last inserted value
	 */
	private void refreshList(boolean p_newValue) {
		int currentIndex = _list_typeRobot.getSelectedIndex();
		int listSize = _list_typeRobot.getModel().getSize();
		
		if (currentIndex == listSize && currentIndex > 0) {
			_list_typeRobot.setSelectedIndex(currentIndex - 1);
		} else if (currentIndex == 0 && listSize == 0) {
			_list_typeRobot.clearSelection();
		} else if (p_newValue) {
			_list_typeRobot.setSelectedIndex(listSize - 1);
		} else if (currentIndex < 0) {
			_list_typeRobot.setSelectedIndex(0);
		}

		_list_typeRobot.repaint();
		_list_typeRobot.revalidate();
	}
	
	/**
	 * Reload robot type list from model (if new robot type)
	 */
	private void reloadList() {
		_list_typeRobot.setModel(new RobotTypeListModel(
			_simulator.getSenario().getRobotTypeList()));
	}
}
